<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
    wrapClassName="ant-modal-cust-warp"
    style="top:5%;height: 85%;overflow-y: hidden">

    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form"  v-bind="layout"  :model="model" :rules="validatorRules">
        <a-form-model-item label="角色编码" prop="roleCode">
          <a-input v-model="model.roleCode" :disabled="model.id ? true : false"  placeholder="请输入角色编码"/>
        </a-form-model-item>
        <a-form-model-item label="角色名称" prop="roleName">
          <a-input v-model="model.roleName" placeholder="请输入角色名称"/>
        </a-form-model-item>
        <a-form-model-item label="角色描述">
          <a-textarea :rows="5" v-model="model.description" placeholder="请输入角色描述(最多150字)" :maxLength="150"/>
        </a-form-model-item>
        <a-form-model-item label="权限列表" prop="roleList">
          <permission-list ref="permissionRef" v-model="model.roleList" />
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
  import { addRole, editRole } from '@/api/api';
  import { getAction } from '@/api/manage';
  import PermissionList from './PermissionList';
  export default {
    name: "RoleModal",
    components: { PermissionList },
    data () {
      return {
        title:"操作",
        visible: false,
        model: {
          id:'',
        },
        layout: {
          labelCol: { span: 3 },
          wrapperCol: { span: 14 },
        },
        confirmLoading: false,
        validatorRules:{
          roleName: [
            { required: true, message: '请输入角色名称!' },
            { min: 2, max: 30, message: '长度在 2 到 30 个字符', trigger: 'blur' }
          ],
          roleCode: [
            { required: true, message: '请输入角色名称!'},
            { min: 0, max: 64, message: '长度不超过 64 个字符', trigger: 'blur' },
            { validator: this.validateRoleCode}
          ],
          roleList: [
            { required: true, message: '请选择权限列表!' }
          ]
        },
        url: {
          list: '/sys/role/list'
        }
      }
    },
    created () {
      //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
        //树形表回填数据
        this.$nextTick(() => {
          this.$refs.permissionRef.loadData(this.model.id);
        })
      },
      close () {
        this.$refs.form.clearValidate();
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        that.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let obj;
            let roleList = that.$refs.permissionRef.handleSubmit();
            let params = Object.assign(that.model, roleList, {roleId: that.model.id});
            delete params.roleList;
            if(!that.model.id){
              params.status = 'Y';
              obj=addRole(params);
            }else{
              obj=editRole(params);
            }
            obj.then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
                that.close();
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }else{
            return false;
          }
        })
      },
      handleCancel () {
        this.close()
      },
      validateRoleCode(rule, value, callback){
        // 编辑不校验
        if(this.model.id){
          callback();
        }
        if(/[\u4E00-\u9FA5]/g.test(value)){
          callback("角色编码不可输入汉字!");
        }else{
          getAction(this.url.list, {roleCode: this.model.roleCode}).then((res) => {
            if(res.success){
              if(res.result && res.result.records && res.result.records.length){
                callback('角色code已存在!');
              }else{
                callback();
              }
            }
          })
        }
      }
    }
  }
</script>